﻿function RepeaterControl() {
    this.HeaderTemplate = "";
    this.ItemTemplate = "";
    this.SubItemTemplate = "";
    this.SelectedItemTemplate = "";
    this.FooterTemplate = "";
    this.AlternatingItemTemplate = "";
    this.SeparatorTemplate = "";
    this.HostControlID = null;
    this.DataSource = null;
}

RepeaterControl.prototype.DataBind = function() {
    var htmlString = "";
    htmlString += repeaterControl.HeaderTemplate;
    for (var count = 0; count < repeaterControl.DataSource.length; count++) {
        var node = repeaterControl.DataSource[count];
        var row = repeaterControl.ItemTemplate;

        if (this.AlternatingItemTemplate != "" && (count + 1) % 2 == 0) {
            row = repeaterControl.AlternatingItemTemplate;
        }

        for (var i = 0; i < node.attributes.length; i++) {
            var re = new RegExp("{@" + node.attributes[i].nodeName + "}", "g");
            row = row.replace(re, node.attributes[i].nodeValue);
        }

        if (repeaterControl.SubItemTemplate != "") {
            var subresult = repeaterControl.SubDataBind(node.childNodes);
            var re = new RegExp("{@subdata}", "g");
            row = row.replace(re, subresult);
        }

        htmlString += row;

        if (repeaterControl.SeparatorTemplate != "" && count + 1 != this.DataSource.length) {
            htmlString += repeaterControl.SeparatorTemplate;
        }

    }
    htmlString += repeaterControl.FooterTemplate;
    $(repeaterControl.HostControlID).html(htmlString);
}

RepeaterControl.prototype.SubDataBind = function(SubDataSource) {
    var htmlString = "";
    for (var count = 0; count < SubDataSource.length; count++) {
        var node = SubDataSource[count];
        var row = repeaterControl.SubItemTemplate;

        if (repeaterControl.AlternatingItemTemplate != "" && (count + 1) % 2 == 0) {
            row = repeaterControl.AlternatingItemTemplate;
        }

        for (var i = 0; i < node.attributes.length; i++) {
            var re = new RegExp("{@" + node.attributes[i].nodeName + "}", "g");
            row = row.replace(re, node.attributes[i].nodeValue);
        }

        htmlString += row;
    }
    return htmlString;
}